IF EXISTS (SELECT * FROM DBO.SYSOBJECTS WHERE NAME='SP_CAPNHATNHANVIEN')
BEGIN
	DROP PROC SP_CAPNHATNHANVIEN
END
GO
CREATE PROC  SP_CAPNHATNHANVIEN
	-- TAT CA THAM SO IN + OUT 
	@HONV	NVARCHAR(10),
	@TENLOT NVARCHAR(20),
	@TENNV	NVARCHAR(10),
	@MANV	VARCHAR(10),
	@NGSINH SMALLDATETIME,
	@DCHI	NVARCHAR(100),
	@PHAI	NVARCHAR(3),
	@LUONG	NUMERIC,
	@MA_NQL VARCHAR(10),
	@PHG	INT,
	@STATUS INT OUTPUT
AS
BEGIN
	IF EXISTS (SELECT * FROM NHANVIEN WHERE MANV=@MANV)
	BEGIN
		--NHAN VIEN DA TON TAI
		UPDATE NHANVIEN
		SET HONV	=@HONV,
			TENLOT	=@TENLOT, 
			TENNV	=@TENNV,	
			MANV	=@MANV,	
			NGSINH	=@NGSINH, 
			DCHI	=@DCHI,	
			PHAI	=@PHAI,	
			LUONG	=@LUONG,	
			MA_NQL	=@MA_NQL, 
			PHG		=@PHG	
		WHERE MANV	=@MANV
		SET @STATUS=1	
		PRINT 'NHAN VIEN DA TON TAI'
	END
	ELSE
	BEGIN
		--NHAN VIEN CHUA TON TAI
		INSERT INTO NHANVIEN
		VALUES
		(
			@HONV,
			@TENLOT, 
			@TENNV,	
			@MANV,	
			@NGSINH, 
			@DCHI,	
			@PHAI,	
			@LUONG,	
			@MA_NQL, 
			@PHG	
		)

		SET @STATUS=0
		PRINT 'NHAN VIEN KO TON TAI'
	END
END

/*
s
SELECT * FROM NHANVIEN
DECLARE @STATUS INT
EXEC SP_CAPNHATNHANVIEN 'NGUYEN','HOANG','BAO','123','02/02/1990','12 CHANH HUNG',
'NAM',20000,'12321',4,@STATUS OUTPUT
SELECT @STATUS AS 'TENNV'

*/